/*
 * jdbc-trace-wrapper
 * Copyright (C) 2010. The jdbc-trace-wrapper Project
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package nz.jdbcwrapper;

import java.sql.SQLException;
import java.sql.Array;
import java.sql.ResultSet;
import java.util.Map;

class WrapperArray implements Array
{
  final Data data;
  final Array array;
  final Statistic statistic;
  final String prefix;
  final long id = Util.id();

  WrapperArray(Data data, Statistic statistic, String parentPrefix, Array array)
  {
    this.data = data;
    this.array = array;
    this.statistic = statistic;
    this.prefix = parentPrefix + "[" + id + "]";
    if (data.trace >= 5)
      Util.trace(prefix + " new Array");
  }

  public String getBaseTypeName() throws SQLException
  {
      return array.getBaseTypeName();
  }

  public int getBaseType() throws SQLException
  {
    return array.getBaseType();
  }

  public Object getArray() throws SQLException
  {
    return array.getArray();
  }

  public Object getArray(Map map) throws SQLException
  {
    return array.getArray(map);
  }

  public Object getArray(long index, int count) throws SQLException
  {
    return array.getArray(index, count);
  }

  public Object getArray(long index, int count, Map map) throws SQLException
  {
    return array.getArray(index, count, map);
  }

  public ResultSet getResultSet() throws SQLException
  {
    long key = 0;

    if (data.trace >= 4)
      key = Util.traceBegin(prefix + " getResultSet()");

    try
    {
      WrapperResultSet resultSet = WrapperResultSet.build(
        data, null, statistic, prefix, array.getResultSet());

      if (data.trace >= 4)
        Util.traceEnd(key, resultSet.prefix);
      return resultSet;
    }
    catch (SQLException e)
    {
      if (data.trace >= 4)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  public ResultSet getResultSet(Map map) throws SQLException
  {
    long key = 0;

    if (data.trace >= 4)
      key = Util.traceBegin(prefix + " getResultSet(Map)");

    try
    {
      WrapperResultSet resultSet = WrapperResultSet.build(
        data, null, statistic, prefix, array.getResultSet(map));

      if (data.trace >= 4)
        Util.traceEnd(key, resultSet.prefix);

      return resultSet;
    }
    catch (SQLException e)
    {
      if (data.trace >= 4)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  public ResultSet getResultSet(long index, int count) throws SQLException
  {
    long key = 0;

    if (data.trace >= 4)
      key = Util.traceBegin(prefix + " getResultSet(" + index + ", " + count + ")");

    try
    {
      WrapperResultSet resultSet = WrapperResultSet.build(
        data, null, statistic, prefix, array.getResultSet(index, count));

      if (data.trace >= 4)
        Util.traceEnd(key, resultSet.prefix);

      return resultSet;
    }
    catch (SQLException e)
    {
      if (data.trace >= 4)
        Util.traceEnd(key, e);
      throw e;
    }
  }

  public ResultSet getResultSet(long index, int count, Map map) throws SQLException
  {
    long key = 0;

    if (data.trace >= 4)
      key = Util.traceBegin(prefix + " getResultSet(" + index + ", " + count + ", Map)");

    try
    {
      WrapperResultSet resultSet = WrapperResultSet.build(
        data, null, statistic, prefix, array.getResultSet(index, count, map));

      if (data.trace >= 4)
        Util.traceEnd(key, resultSet.prefix);

      return resultSet;
    }
    catch (SQLException e)
    {
      if (data.trace >= 4)
        Util.traceEnd(key, e);
      throw e;
    }
  }

//  public void free() throws SQLException
//  {
//      long key = 0L;
//
//      if (data.trace >= 4)
//        key = Util.traceBegin(prefix + " free()");
//
//      try
//      {
//        array.free();
//        if (data.trace >= 3)
//          Util.traceEnd(key, (String)null);
//      }
//      catch (SQLException e)
//      {
//        if (data.trace >= 3)
//          Util.traceEnd(key, e);
//        throw e;
//      }
//  }
}
